﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace QunFaExpert.Data
{
    /// <summary>
    /// Tweet id for all types of tweet
    /// </summary>
    public class TypedId
    {
        public string id { set; get; }
        public WeiBeeCommon.Core.WeiBeeType type { set; get; }
        private Dictionary<string, WeiBeeCommon.Core.WeiBeeType> domaindict = new Dictionary<string, WeiBeeCommon.Core.WeiBeeType>()
        {
            {"WEIBO.COM",WeiBeeCommon.Core.WeiBeeType.Sina },
            {"T.QQ.COM",WeiBeeCommon.Core.WeiBeeType.QQ },
            {"T.SOHU.COM",WeiBeeCommon.Core.WeiBeeType.Sohu }
        };

        /// <summary>
        /// Construct a TypedId by the Url provided. 
        /// </summary>
        /// <param name="url">An url can get by click the tweet time, and copied from the url browser</param>
        public TypedId(string url)
        {
            Regex regex = new Regex(@"http://(([a-z]|\.)+)?");
            var match = regex.Match(url);
            if (match.Success)
            {
                type = domaindict[match.Groups[1].ToString().ToUpper()];
            }
            if (type == WeiBeeCommon.Core.WeiBeeType.Sina)
            {
                id = TweeterUrlToId(url);
            }
            else if (type == WeiBeeCommon.Core.WeiBeeType.Sohu)
            {
                Regex sohuidregex = new Regex(@"(\d+)");
                var sohumatch = sohuidregex.Match(url);
                id = sohumatch.Groups[0].ToString();
            }
            else // if (type == WeiBeeCommon.Core.WeiBeeType.QQ)
            {
                throw new NotImplementedException("QQ type not implemented");
            }
        }

        /// <summary>
        /// 将某一条新浪微博地址转换成这条微博的ID
        /// </summary>
        /// <param name="url">一条微博地址</param>
        /// <returns>微博ID</returns>
        public string TweeterUrlToId(string url)
        {
            url = url.Trim().Substring(url.LastIndexOf("/") + 1);//取得url地址后的62进制数【5KD0TcOcJsZ】
            string[] surl = new string[3];
            surl[2] = String60To10(url.Substring(url.Length - 4));//倒数第三段，取4位，转10进制
            surl[1] = String60To10(url.Substring(url.Length - 8, 4));//倒数第二段，取4位，转10进制
            surl[0] = String60To10(url.Substring(0, url.Length - 8));//第一段，取剩下位数，转10进制
            surl[2] = PadLeftToLengthSeven(surl[2]);
            surl[1] = PadLeftToLengthSeven(surl[1]);

            return surl[0] + surl[1] + surl[2];//合并三段，返回
        }

        /// <summary>
        /// 用0补齐7位长度
        /// </summary>
        /// <param name="str">源串可能长度小于7</param>
        /// <returns>返回长度为7的串</returns>
        private string PadLeftToLengthSeven(string str)
        {
            if (str.Length < 7)
            {
                str = str.PadLeft(7, '0');
            }
            return str;
        }

        private string String60To10(string str62)
        {//62进制转10进制
            Int64 i64 = 0;
            for (int i = 0; i < str62.Length; i++)
            {
                Int64 Vi = (Int64)Math.Pow(62, (str62.Length - i - 1));
                char t = str62[i];
                i64 += Vi * String62Dictionary(t.ToString());
            }
            return i64.ToString();
        }
        private Int64 String62Dictionary(string ks)//62进制字典
        {
            string[] keys = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            int i = 0;
            foreach (string k in keys)
            {
                if (ks == k) { return i; }
                i++;
            }
            return 0;
        }

    }
}
